/*********************\
* Author:Dzlua
* QQ:505544956
* Time:2017/04/09
\**********************/
#include "LuaEngine.h"

namespace DuiLib {
namespace LuaEngine {

int LuaNewWindow(LuaState *L)
{
    LuaStack args(L);
    SDTString name = LuaHelper::GetTString(args[1]);
    if (!name.empty()) {
        SDWindow* wnd =  new SDWindow(name.c_str());
        if (wnd) {
            SDPtr->AddWindow(wnd, true);
            LuaHelperNS::PushCObj(L, wnd);
            return 1;
        }
    }
    L->PushNil();
    return 1;
}

int AddWindow(LuaState *L)
{
    LuaStack args(L);
    SDWindow* wnd = (SDWindow*) LuaHelperNS::GetCObjectData(args[1]);
    if (!wnd) {
        L->PushNil();
        return 1;
    }
    SDPtr->AddWindow(wnd, true);
    L->PushValue(args[1]);
    return 1;
}

int RemoveWindow(LuaState *L)
{
    LuaStack args(L);
    SDWindow* wnd = (SDWindow*) LuaHelperNS::GetCObjectData(args[1]);
    if (wnd) {
        SDPtr->RemoveWindow(wnd);
    }
    return 0;
}

void RemoveAllWindow()
{
    SDPtr->RemoveAllWindow();
}

int WindowsCount()
{
    return SDPtr->WindowsCount();
}

int WindowAt(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsInteger()){
        SDWindow* wnd = SDPtr->WindowAt(args[1].GetInteger());
        if (wnd) {
            LuaHelperNS::PushCObj(L, wnd);
            return 1;
        }
    }
    L->PushNil();
    return 1;
}

int FindWindow(LuaState *L)
{
    LuaStack args(L);
    SDTString name = LuaHelper::GetTString(args[1]);
    if (!name.empty()) {
        SDWindow* wnd = SDPtr->FindWindow(name.c_str());
        if (wnd) {
            LuaHelperNS::PushCObj(L, wnd);
            return 1;
        }
    }
    L->PushNil();
    return 1;
}

int /*HINSTANCE*/ GetInstance()
{
    return (int)SDPtr->GetInstance();
}

int GetInstancePath(LuaState *L)
{
    SDTString str = SDPtr->GetInstancePath().GetData();
    LuaHelperNS::PushTString(L, str);
    return 1;
}

int GetCurrentPath(LuaState *L)
{
    SDTString str = SDPtr->GetCurrentPath().GetData();
    LuaHelperNS::PushTString(L, str);
    return 1;
}

int /*HINSTANCE*/ GetResourceDll()
{
    return (int)SDPtr->GetResourceDll();
}

int GetResourcePath(LuaState *L)
{
    SDTString str = SDPtr->GetResourcePath().GetData();
    LuaHelperNS::PushTString(L, str);
    return 1;
}

int GetResourceZip(LuaState *L)
{
    SDTString str = SDPtr->GetResourceZip().GetData();
    LuaHelperNS::PushTString(L, str);
    return 1;
}

int GetResourceZipPwd(LuaState *L)
{
    SDTString str = SDPtr->GetResourceZipPwd().GetData();
    LuaHelperNS::PushTString(L, str);
    return 1;
}

bool IsCachedResourceZip()
{
    return SDPtr->IsCachedResourceZip();
}

int /*HANDLE*/ GetResourceZipHandle()
{
    return (int)SDPtr->GetResourceZipHandle();
}

void SetInstance(int /*HINSTANCE*/ hInst)
{
    SDPtr->SetInstance((HINSTANCE)hInst);
}

int SetCurrentPath(LuaState *L)
{
    LuaStack args(L);
    SDTString str = LuaHelperNS::GetTString(args[1]);
    SDPtr->SetCurrentPath(str.c_str());
    return 0;
}

void SetResourceDll(int /*HINSTANCE*/ hInst)
{
    SDPtr->SetResourceDll((HINSTANCE)hInst);
}

int SetResourcePath(LuaState *L)
{
    LuaStack args(L);
    SDTString str = LuaHelperNS::GetTString(args[1]);
    SDPtr->SetResourcePath(str.c_str());
    return 0;
}

/*void SetResourceZip(LPVOID pVoid, unsigned int len, LPCTSTR password = NULL);
void SetResourceZip(LPCTSTR pstrZip, bool bCachedResourceZip = false, LPCTSTR password = NULL);*/
int SetResourceZip(LuaState *L)
{
    LuaStack args(L);
    LuaObject tab = args[1];
    if (tab.IsTable()) {
        SDTString zip = LuaHelperNS::GetTString(tab.GetByName("zip"));
        bool cached = LuaHelperNS::GetBool(tab.GetByName("cached"), false);
        SDTString password = LuaHelperNS::GetTString(tab.GetByName("pwd"));
        LPVOID pVoid = LuaHelperNS::GetLPVoid(tab.GetByName("pointer"));
        unsigned int len = LuaHelperNS::GetUInt(tab.GetByName("len"));

        LPCTSTR pwd = NULL;
        if (!password.empty()) pwd = password.c_str();

        if (!zip.empty()) {
            SDPtr->SetResourceZip(zip.c_str(), cached, pwd);
        } else if (pVoid != 0 && len > 0) {
            SDPtr->SetResourceZip(pVoid, len, pwd);
        }
    }
    return 0;
}

void SetResourceType(int nType)
{
    SDPtr->SetResourceType(nType);
}

int GetResourceType()
{
    return SDPtr->GetResourceType();
}

//bool GetHSL(short* H, short* S, short* L);
int GetHSL(LuaState *L)
{
    short h, s, l;
    bool bRet = SDPtr->GetHSL(&h, &s, &l);

    LuaObject tab;
    tab.AssignNewTable(L);
    tab.SetInteger("h", (int)h);
    tab.SetInteger("s", (int)s);
    tab.SetInteger("l", (int)l);

    L->PushBoolean(bRet);
    tab.Push();
    return 2;
}

// H:0~360, S:0~200, L:0~200 
void SetHSL(bool bUseHSL, short H, short S, short L)
{
    SDPtr->SetHSL(bUseHSL, H, S, L);
}

void ReloadSkin()
{
    SDPtr->ReloadSkin();
}

/*CPaintManagerUI* GetPaintManager(LPCTSTR pstrName);
CStdPtrArray* GetPaintManagers();*/

int LoadPlugin(LuaState *L)
{
    LuaStack args(L);
    SDTString str = LuaHelperNS::GetTString(args[1]);
    bool bRet = SDPtr->LoadPlugin(str.c_str());
    L->PushBoolean(bRet);
    return 1;
}

//CStdPtrArray* GetPlugins();

void MessageLoop()
{
     SDPtr->MessageLoop();
}

/*bool TranslateMessage(const LPMSG pMsg);
void Term();*/

void ReloadSharedImages()
{
    SDPtr->ReloadSharedImages();
}

void SetAllDPI(int iDPI)
{
    SDPtr->SetAllDPI(iDPI);
}

void Register(LuaState *L)
{
    LuaObject sd = L->GetGlobals().CreateTable("SD");
    sd.Register("NewWindow", LuaNewWindow);

    sd.Register("AddWindow", AddWindow);
    sd.Register("RemoveWindow", RemoveWindow);
    sd.RegisterDirect("RemoveAllWindow", RemoveAllWindow);
    sd.RegisterDirect("WindowsCount", WindowsCount);
    sd.Register("WindowAt", WindowAt);
    sd.Register("FindWindow", FindWindow);

    sd.RegisterDirect("GetInstance", GetInstance);
    sd.Register("GetInstancePath", GetInstancePath);
    sd.Register("GetCurrentPath", GetCurrentPath);
    sd.RegisterDirect("GetResourceDll", GetResourceDll);
    sd.Register("GetResourcePath", GetResourcePath);
    sd.Register("GetResourceZip", GetResourceZip);
    sd.Register("GetResourceZipPwd", GetResourceZipPwd);
    sd.RegisterDirect("IsCachedResourceZip", IsCachedResourceZip);
    sd.RegisterDirect("GetResourceZipHandle", GetResourceZipHandle);
    sd.RegisterDirect("SetInstance", SetInstance);
    sd.Register("SetCurrentPath", SetCurrentPath);
    sd.RegisterDirect("SetResourceDll", SetResourceDll);
    sd.Register("SetResourcePath", SetResourcePath);
    sd.Register("SetResourceZip", SetResourceZip);
    sd.RegisterDirect("SetResourceType", SetResourceType);
    sd.RegisterDirect("GetResourceType", GetResourceType);
    sd.Register("GetHSL", GetHSL);
    sd.RegisterDirect("SetHSL", SetHSL);
    sd.RegisterDirect("ReloadSkin", ReloadSkin);

    sd.RegisterDirect("LoadPlugin", LoadPlugin);

    sd.RegisterDirect("MessageLoop", MessageLoop);

    sd.RegisterDirect("ReloadSharedImages", ReloadSharedImages);
    sd.RegisterDirect("SetAllDPI", SetAllDPI);
    
}

void UnRegister(LuaState *L)
{
    L->GetGlobals().SetNil("SD");
}

}
}